package org.common.utils.generatormapper.event.handler.mysqlconnect;
import org.common.utils.database.access.DataAccess;
import org.common.utils.database.access.DataAccessTool;
import org.common.utils.event.annotation.EventMethod;
import org.common.utils.generatormapper.GeneratorMapperManager;
import org.common.utils.generatormapper.event.entity.MysqlConnectEvent;
import org.common.utils.generatormapper.window.tools.DialogTool;
import org.common.utils.tuple.TupleObject3;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.swing.*;
import java.util.List;
import java.util.concurrent.CompletableFuture;

public class MysqlConnectHandler {

    private static final Logger log = LoggerFactory.getLogger(MysqlConnectHandler.class);

    /**
     * 数据库连接中
     */
    private boolean dbConnecting = false;

    /**
     * mysql连接事件处理
     * @param event 事件对象
     */
    @EventMethod
    public synchronized void mysqlConnect(MysqlConnectEvent event) {
        if(dbConnecting) {
            DialogTool.showMessageDialog("GeneratorEntityWindow.dialog.mysql.start.connect");
            return;
        }

        if (DataAccessTool.get().isInit()) {
            DialogTool.showMessageDialog("GeneratorEntityWindow.dialog.mysql.already.connect");
            return;
        }

        TupleObject3<String, String, String> arg = checkArg(event);
        if (arg == null) {
            return;
        }

        String host = arg.getOne();
        String username = arg.getTwo();
        String password = arg.getThree();

        this.dbConnecting = true;
        CompletableFuture.runAsync(() -> {
            DataAccessTool.get().init(host, username, password);
            DataAccess dataAccess = DataAccessTool.get().getDataAccess();

            List<String> tableList = dataAccess.select("show tables", (result) -> {
                return result.getString(1);
            });

            DefaultListModel<String> defaultListModel = new DefaultListModel<>();
            tableList.forEach(defaultListModel::addElement);

            JList<String> tblList = GeneratorMapperManager.getInstance().getComponent(JList.class, "tblList");
            tblList.setModel(defaultListModel);
            this.dbConnecting = false;
        });
    }

    private TupleObject3<String, String , String> checkArg(MysqlConnectEvent event) {
        String host = event.getTxtHost().getText();
        if(host != null) {
            //等于示例字段串
            String string = GeneratorMapperManager.getInstance().getString("GeneratorEntityWindow.txtMysqlHost.example.text");
            if(host.equals(string))
                host = null;
        }

        if(host == null || host.isEmpty()) {
            DialogTool.showMessageDialog("GeneratorEntityWindow.dialog.mysql.host.empty");
            return null;
        }

        String username = event.getTxtUsername().getText();
        if(username == null || username.isEmpty()) {
            DialogTool.showMessageDialog("GeneratorEntityWindow.dialog.mysql.username.empty");
            return null;
        }

        String password = new String(event.getTxtPassword().getPassword());
        if(password.isEmpty()) {
            DialogTool.showMessageDialog("GeneratorEntityWindow.dialog.mysql.password.empty");
            return null;
        }
        return TupleObject3.of(host , username , password);
    }
}
